package com.qdaily.ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;

import com.qdaily.supportlibrary.app.XActivity;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.controller.ActivityController;
import com.qdaily.ui.data.QDConfigManager;
import com.qdaily.ui.data.UserInformationManager;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.QDEnum;
import com.qdaily.ui.net.QdailyNetwork;
import com.qdaily.ui.ui.views.QDProgressDialog;
import com.qdaily.ui.util.AnalyticsHelper;
import com.qdaily.ui.util.BusProvider;
import com.qdaily.ui.util.ToastUtil;

/**
 * Activity管理基类
 * Created by Felix on 4/1/15.
 */
public abstract class QDBaseActivity extends XActivity implements QdailyNetwork.OnRequestCallBackListener {

    private QdailyNetwork qdailyNetWork;
    private QDProgressDialog qdProgressDialog;
    protected final String QDTAG = getClass().getSimpleName();

    protected QDEnum.EActivityAnim mActivityAnim;
    public static final String KEY_ActivityAnim = "KEYActivityAnim";

    protected final int REQUEST_IMAGE = 142;
    protected final int REQUEST_CAMERA = 143;
    protected final int REQUEST_IMAGECROP = 145;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityAnim = (QDEnum.EActivityAnim) getIntent().getSerializableExtra(KEY_ActivityAnim);
        // Register ourselves so that we can provide the initial value.
        BusProvider.getInstance().register(this);

        //get the cookie ,for evertime
        NetConfigs.getCookie(getUserInformationManager().getUserInformation().getCookie());
        ActivityController.getInstance().addActivity(this);
        QLog.i(QDTAG, "onCreate");
    }

    @Override
    protected void onResume() {
        QLog.i(QDTAG, "onResume");
        AnalyticsHelper.getInstance().onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        QLog.i(QDTAG, "onPause");
        AnalyticsHelper.getInstance().onPause(this);
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        QLog.i(QDTAG, "onDestroy");
        // Always unregister when an object no longer should be on the bus.
        BusProvider.getInstance().unregister(this);
        ActivityController.getInstance().removeActivity(this);
        super.onDestroy();
    }

    public QdailyNetwork getRequestManager() {
        if (qdailyNetWork == null) {
            qdailyNetWork = new QdailyNetwork(this, this);
        }
        QLog.w("FELIX", "getRequestManager");
        return qdailyNetWork;
    }

    public QDConfigManager getQDConfigManager() {
        return QDConfigManager.getInstance();
    }

    public QDProgressDialog getQDProgressDialog() {
        if (qdProgressDialog == null) {
            qdProgressDialog = new QDProgressDialog(this);
        }
        return qdProgressDialog;
    }

    public UserInformationManager getUserInformationManager() {
        return UserInformationManager.getInstance();
    }

    @Override
    protected String getCloseWarning() {
        return "再按一次退出应用";
    }

    @Override
    protected int getFragmentContainerId() {
        return 0;
    }

    @Override
    public void onResponse(NetUtil.NetParams params, JsonData response) {
        onRequestCallBack(params, response);
    }

    @Override
    public void onResponseError(NetUtil.NetParams params, Object response) {
        onRequestErrorCallBack(params, response);
    }

    @Override
    public void onResponseCache(NetUtil.NetParams params, JsonData response) {

    }

    /**
     * @param params   请求时候发出的参数
     * @param response 回调数据
     * @Desription 服务接口回调
     */
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
    }

    protected void onRequestErrorCallBack(NetUtil.NetParams params, Object response) {
    }

    /**
     * 吐司弹窗
     */
    public void displayToast(String toastConetnt) {
        ToastUtil.showToast(this, toastConetnt);
    }

    public void displayToast(int toastConetntRes) {
        ToastUtil.showToast(this, toastConetntRes);
    }

    public ProgressDialog loadingDialog;
    protected int loadStringId = R.string.loading;

    public void showLoading() {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);
        }
        loadingDialog.setMessage(getString(loadStringId));
        loadingDialog.show();
    }

    public void showLoading(String str) {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(this);

        }
        loadingDialog.setMessage(str);
        loadingDialog.show();
    }

    public void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (mActivityAnim != null) {
            configActivityAnim(mActivityAnim, false);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        QDEnum.EActivityAnim curActivityAnim = (QDEnum.EActivityAnim) intent.getSerializableExtra(KEY_ActivityAnim);
        if (curActivityAnim != null) {
            configActivityAnim(curActivityAnim, true);
        }
    }

    private void configActivityAnim(QDEnum.EActivityAnim eActivityAnim, boolean isIn) {
        switch (eActivityAnim) {
            case SlideFromRight:
                if (isIn) {
                    overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                } else {
                    overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
                }
                break;
            case SlideFromBottom:
                if (isIn) {
                    overridePendingTransition(R.anim.slide_bottom_in, R.anim.loading_exit_anim);
                } else {
                    overridePendingTransition(R.anim.loading_enter_anim, R.anim.slide_bottom_out);
                }
                break;
            case Fade:
                overridePendingTransition(R.anim.loading_enter_anim, R.anim.loading_exit_anim);
                break;
        }
    }
}
